<?php

namespace app\backend\controller;

use app\common\service\sys\UserService;
use app\common\controller\Basic;
use app\common\service\fcm\BorrowService;
use app\common\service\pmm\FlowBillService;
use app\common\service\pmm\FlowChaosongService;
use app\common\service\sys\MenuService;
use app\common\service\xzm\AnnouncementService;
use app\common\service\xzm\NewsService;
use think\facade\Db;
use Exception;
use app\common\service\pmm\FlowBrowsingService;
use app\common\service\pmm\ConfigService;

/**
 * 首页，因Index无表无业务层，故业务写在控制器里
 * @author kaige
 */
class Index extends Basic
{
    // 初始化
    protected function initialize()
    {
        parent::initialize();
        // 当前控制器服务实例化
        $this->service = new MenuService();
    }

    /**
     * 登出
     */
    public function logout()
    {
        $result = UserService::toLogout($this->user['id']);
        return json($result);
    }

    public function index()
    {
        if (request()->isGet()) {
            $menu = $this->service->getMenuDataTree($this->user['id']);
            $config  = [
                'id'              => $this->user['id'],
                'username'        => $this->user['un'],
                'avatar'          => _img($this->user['avatar']),
                'nickname'        => $this->user['real_name'],
                'phone'           => $this->user['phone'],
                'last_ip'         => $this->user['last_ip'],
                'last_time'       => date("Y-m-d H:i", $this->user['last_time']),
                'login_num'       => $this->user['ln']
            ];

            $outdata = [
                'title' => _conf('site_title'),
                'site_name' => _conf('app_name'),
                'app_version' => _conf('app_version'),
                'site_copy' => _conf('site_copy'),
                'menus' => $this->get_view_menu($menu['menu']),
                'leftmenu' => $this->get_left_menu($menu['leftmenu']),
                'config' => $config,
            ];
            $outdata['dict'] = $this->sysdict;
            return _jok('ok', $outdata);
        }
        return _jerr(1000);
    }


    /**
     * 迎合前端菜单
     */
    protected function get_view_menu($array)
    {
        $str = "[";
        foreach ($array as $vo) {
            $str .= '{"id":"' . (isset($vo["menu_id"]) ? $vo["menu_id"] : "") . '"';
            $str .= ',"title":"' . (isset($vo["title"]) ? $vo["title"] : "") . '"';
            $tmp = isset($vo["url"]) ? $vo["url"] : "";
            if ($tmp) {
                $tmp = str_replace("backend", "pages", $tmp);
                $tmp = str_replace(".", "/", $tmp);
                $tmp = str_replace("/html", ".html", $tmp);
            }
            $str .= ',"pageURL":"' . $tmp . '"';
            $str .= ',"name":"' . (isset($vo["name"]) ? $vo["name"] : "") . '"';
            $str .= ',"icon":"' . (isset($vo["icon"]) ? $vo["icon"] : "") . '"';
            $str .= ',"openType":"' . (isset($vo["open_type"]) ? $vo["open_type"] : "") . '"';
            $str .= ',"width":"' . (isset($vo["width"]) ? $vo["width"] : "") . '"';
            $str .= ',"height":"' . (isset($vo["height"]) ? $vo["height"] : "") . '"';
            $str .= ',"maxopen":"' . (isset($vo["is_max"]) ? $vo["is_max"] : "") . '"';
            $str .= ',"extend":"' . ((isset($vo["is_extend"]) && $vo["is_extend"] == 1) ? "true" : "false") . '"';
            $getsubm = '';
            if (!empty($vo["children"])) {
                $getsubm = $this->get_view_menu($vo["children"]);
            }
            $getsubm = rtrim($getsubm, ",]") . ']';
            if (strlen($getsubm) > 10) {
                $str .= ',"childs":' . $getsubm . '';
            } else {
                $str .= ',"childs":null';
            }
            $str .= '},';
        }
        $str .= ']';
        $str = rtrim($str, ",]") . ']';
        return $str;
    }

    /**
     * 左侧菜单
     */
    protected function get_left_menu($array)
    {
        $str = "[";
        foreach ($array as $vo) {
            $str .= '{"id":"' . (isset($vo["menu_id"]) ? $vo["menu_id"] : "") . '"';
            $str .= ',"title":"' . (isset($vo["title"]) ? $vo["title"] : "") . '"';
            $str .= ',"pageURL":"' . (isset($vo["node"]) ? $vo["node"] : "") . '"';
            $str .= ',"name":"' . (isset($vo["name"]) ? $vo["name"] : "") . '"';
            $str .= ',"icon":"' . (isset($vo["icon"]) ? $vo["icon"] : "") . '"';
            $str .= ',"openType":"' . (isset($vo["open_type"]) ? $vo["open_type"] : "") . '"';
            $str .= ',"width":"' . (isset($vo["width"]) ? $vo["width"] : "") . '"';
            $str .= ',"height":"' . (isset($vo["height"]) ? $vo["height"] : "") . '"';
            $str .= ',"maxopen":"' . (isset($vo["is_max"]) ? $vo["is_max"] : "") . '"';
            $str .= '},';
        }
        $str .= ']';
        $str = rtrim($str, ",]") . ']';
        return $str;
    }

    /**
     * 仪表页
     */
    public function welcome()
    {
        return _jok('ok', []);
    }

    /**
     * 首页快捷菜单
     */
    public function _homemenu()
    {
        $menus = new MenuService();
        $where = [];
        $where[] = ['status', '=', 1];
        $where[] = ['is_home_entry', '=', 1];
        $list = $menus->getLists(1, 12, $where);
        $list = $list['data'];
        foreach ($list as $k => $vo) {
            $tmp = isset($vo["node"]) && !empty($vo["node"]) ?  url($vo['node']) : "";
            if ($tmp) {
                $tmp = str_replace("backend", "pages", $tmp);
                $tmp = str_replace(".", "/", $tmp);
                $tmp = str_replace("/html", ".html", $tmp);
            }
            $list[$k]['url'] = $tmp;
        }
        //取流程条数
        return $list;
    }

    //集一起，一次输出
    public function welcomelist()
    {
        if ($this->request->isGet()) {
            $arr = [];
            $arr['homemenu'] = _layui(); // $this->_homemenu();
            $arr['myshenqing'] = $this->_myshenqing();
            $arr['announcement'] = $this->_announcement();
            $arr['homenews'] = $this->_homenews();
            $arr['chaosongmy'] =  _layui(); // $this->_chaosongmy();
            $arr['meeting'] =  _layui(); // $this->_meeting();
            return _jok('', $arr);
        }
        return _jerr(1000);
    }

    /**
     * 我的申请
     */
    public function _myshenqing()
    {
        $out = [];
        try {
            $fbs = new FlowBillService();
            $data['user_id'] = $this->user['id'];
            $data['isfilter_my'] = 1; //过滤我的。
            $data['page'] = 1;
            $data['limit'] = 7;
            $data['is_flow'] = 1;
            $out = $fbs->_list($data);
        } catch (Exception $exception) {
            $out = _layui();
        }
        return $out;
    }

    /**
     * 通知公告
     */
    public function _announcement()
    {
        $out = [];
        try {
            $fbs = new AnnouncementService();
            $bows = new FlowBrowsingService();
            $data['user_id'] = $this->user['id'];
            $data['page'] = 1;
            $data['limit'] = 6;
            $data['index_show_user'] = 1;
            $out = $fbs->_flow_list($data);
            if ($out['data']) {
                foreach ($out['data'] as $k => $v) {
                    if (!empty($v['put_time'])) {
                        $out['data'][$k]['put_time'] = time2str($v['put_time'], 2);
                    }
                    //是否已读
                    if ($bows->is_read_log($v['id'], $data['user_id'])) {
                        $out['data'][$k]['is_read'] = 1;
                    }
                }
            }
        } catch (Exception $exception) {
            $out = _layui();
        }
        return $out;
    }

    /**
     * 新闻资讯
     */
    public function _homenews()
    {
        $out = [];
        try {
            $newss = new NewsService();
            $bows = new FlowBrowsingService();
            $data['user_id'] = $this->user['id'];
            $data['page'] = 1;
            $data['limit'] = 9;
            $data['index_show_user'] = 1;
            $out = $newss->_flow_list($data);
            if ($out['data']) {
                foreach ($out['data'] as $k => $v) {
                    if (!empty($v['put_time'])) {
                        $out['data'][$k]['put_time'] = time2str($v['put_time'], 2);
                    }
                    //是否已读
                    if ($bows->is_read_log($v['id'], $data['user_id'])) {
                        $out['data'][$k]['is_read'] = 1;
                    }
                }
            }
        } catch (Exception $exception) {
            $out = _layui();
        }
        return $out;
    }


    /**
     * 抄送给我
     */
    public function _chaosongmy()
    {
        $out = [];
        try {
            $fcs = new FlowChaosongService();
            $data = [];
            $data['user_id'] = $this->user['id'];
            $out = $fcs->_list($data);
        } catch (Exception $exception) {
            $out = _layui();
        }
        return $out;
    }


    /**
     * 今日会议
     */
    public function _meeting()
    {
        $out = [];
        try {
            $fbs = new FlowBillService();
            $data['user_id'] = $this->user['id'];
            $data['modu_id'] = 11;
            $data['page'] = 1;
            $data['limit'] = 7;
            $out = $fbs->_list($data);
        } catch (Exception $exception) {
            $out = _layui();
        }
        return $out;
    }
}
